package com.sxyjhh.service.data_exchange;

import com.sxyjhh.common.DBConfig;
import com.sxyjhh.dbutil.basic.ConstConfig;
import com.sxyjhh.dbutil.basic.DBQueryUtils;
import com.sxyjhh.dbutil.utils.DBInfoUtil;
import com.sxyjhh.schedule.TaskRunnable;
import com.sxyjhh.service.insert_csafetycertificate.InsertCsafetycertificate_InsertCsafetycertificatePort_Client;
import com.sxyjhh.utils.CommonUtils;
import com.sxyjhh.utils.IdGenerator;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Administrator on 2018/6/14.
 */
//企业安全生产许可信息数据推送
public class PollingRunnableQyxy extends TaskRunnable {

    private String tabName;
    private String json;
    private int ZJNUM ;
    private CountDownLatch countDownLatch ;
    private List<Map<String, Object>> errorList;
    private Map<String, Object> dataMap;

    private String recordIp = DBConfig.readPop("output.record.ip");
    private String recordDbName = DBConfig.readPop("output.record.db.name");
    private String recordUserName = DBConfig.readPop("output.record.username");
    private String recordPassword = DBConfig.readPop("output.record.password");
    private String recordPort = DBConfig.readPop("output.record.port");
    private String recordDbSType = DBConfig.readPop("output.record.type");

    private static final Logger log = Logger.getLogger(PollingRunnableQyxy.class);


    public PollingRunnableQyxy(String id,String tabName,String json,int ZJNUM,
                               CountDownLatch countDownLatch,List<Map<String, Object>> errorList,
                               Map<String, Object> dataMap) {
        this.taskId = "polling-" + id;
        this.tabName = tabName;
        this.json = json;
        this.ZJNUM = ZJNUM;
        this.countDownLatch = countDownLatch;
        this.errorList = errorList;
        this.dataMap = dataMap;
    }

    @Override
    public  void run() {
        log.info("——————————————————————ThreadName————————————————————————");
        log.info(Thread.currentThread().getName());
        log.info("——————————————————————TaskName————————————————————————");
        log.info(taskId);
        log.info("——————————————————————possTime————————————————————————");
        log.info(CommonUtils.YYYYMMDDHHMMSS.format(new Date()));
        log.info(json);
        String result = InsertCsafetycertificate_InsertCsafetycertificatePort_Client.push(json);
        if (!result.contains("200")) {
            errorRecord(json, result, tabName);
            errorList.add(dataMap);
        }

        countDownLatch.countDown();

        System.out.println(result);
        System.out.println("推送编号："+ZJNUM);
    }

    private void errorRecord(String json, String resCode, String tableName) {
        String url = "";
        ConstConfig.DBType dbType = ConstConfig.DBType.valueOf(Integer.valueOf(recordDbSType));
        if ("3".equals(recordDbSType)) {
            url = "jdbc:mysql://" + recordIp + ":" + recordPort + "/" + recordDbName + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useInformationSchema=true&useSSL=false";
        } else {
            url = DBInfoUtil.getDBUrl(dbType, recordIp, recordPort, recordDbName);
        }

        DBQueryUtils dbQueryUtils = new DBQueryUtils(DBInfoUtil.getDBDriverName(dbType),
                url, recordUserName, recordPassword, 5);

        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", IdGenerator.createNewId());
        map.put("task_id", "");
        map.put("time", com.sxyjhh.dbutil.utils.CommonUtils.YYYYMMDDHHMMSS.format(new Date()));
        map.put("error_message", resCode);
        map.put("error_data", json);
        map.put("table_name", tableName);
        dataList.add(map);
        try {
            dbQueryUtils.insertBatchData("data_exchange_error", recordDbName, dataList);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
